import React, { Component } from 'react';
import { Link, routerRedux } from 'dva/router';
import { connect } from 'dva';

import { List, InputItem, Button, Toast, Flex,Icon,TabsInfo,WingBlank } from 'antd-mobile';
import { createForm } from 'rc-form';
import NavTopBar from '../../components/NavTopBar/NavTopBar';
import MyScroll from '../../components/MyScroll/MyScroll';
import weixinsvg from '../../assets/img/weixin.svg';
import weibosvg from '../../assets/img/weibo.svg';
import qqsvg from '../../assets/img/QQ.svg';
import { apiURl } from '../../utils/common';
//import { common } from 'common';

const { Item } = List;


class PayPwd extends Component {
    constructor(props) {
        super(props);
        this.state={

        }

    }

    onSubmit = () => {
        const { newPwd,newPwd1 } = this.props.form.getFieldsValue();
        var reg = new RegExp("^[0-9]*$");
        if (!newPwd) {
            Toast.info('请输入新密码', 1.5);
            return;
        }
        if (!newPwd1) {
            Toast.info('请确认新密码',1.5);
            return;
        }
        console.log(!reg.test(newPwd)||!reg.test(newPwd1))

        if(newPwd!=newPwd1){
            Toast.offline('两次新密码不一样',1.5);
            return;
        }
        if(!reg.test(newPwd)||!reg.test(newPwd1)) {Toast.info('密码必须为6位数字', 1.5);return}
        this.props.dispatch({ type: 'login/PayPwdEFF', obj: {  newPwd } }).then((r)=>{
            if (r.code == 200) {
                Toast.success(r.msg, 1.5, ()=>{
                    this.props.dispatch(routerRedux.replace({
                        pathname: '/',
                    }))
                });
            } else {
                Toast.fail(r.msg, 1.5);
            }
        });

    }
    render() {
        const { getFieldProps, getFieldError } = this.props.form;
        return (
            <Flex style={{
                height:"100%"
            }} direction="column"

            >
                <NavTopBar title="修改支付密码" dispatch={this.props.dispatch}/>
                <form style={{width:"100%"}}>
                    <List
                        renderFooter={() => getFieldError('newPwd') && getFieldError('newPwd').join(',')}
                    >

                        <InputItem {...getFieldProps('newPwd')} placeholder="请输入新密码" type="password" maxLength={6}>新密码</InputItem>
                        <InputItem {...getFieldProps('newPwd1')} placeholder="请确认新密码" type="password" maxLength={6}>确认密码</InputItem>

                        <Item style={{ backgroundColor: 'unset', marginTop: '.3rem' }}>
                            <Button type="primary" onClick={this.onSubmit} style={{borderRadius:".5rem"}}>设置</Button>
                        </Item>

                    </List>
                </form>


            </Flex>
        );
    }
}

export default connect(({ login }) => ({ login }))(createForm()(PayPwd));
